<!DOCTYPE html>

<html>
<head>
  <title>io.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="analyze.html">
                analyze.coffee
              </a>
            
              
              <a class="source" href="autoload.html">
                autoload.coffee
              </a>
            
              
              <a class="source" href="blender.html">
                blender.coffee
              </a>
            
              
              <a class="source" href="calculate.html">
                calculate.coffee
              </a>
            
              
              <a class="source" href="caman.html">
                caman.coffee
              </a>
            
              
              <a class="source" href="convert.html">
                convert.coffee
              </a>
            
              
              <a class="source" href="event.html">
                event.coffee
              </a>
            
              
              <a class="source" href="filter.html">
                filter.coffee
              </a>
            
              
              <a class="source" href="io.html">
                io.coffee
              </a>
            
              
              <a class="source" href="layer.html">
                layer.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="module.html">
                module.coffee
              </a>
            
              
              <a class="source" href="pixel.html">
                pixel.coffee
              </a>
            
              
              <a class="source" href="plugin.html">
                plugin.coffee
              </a>
            
              
              <a class="source" href="renderer.html">
                renderer.coffee
              </a>
            
              
              <a class="source" href="store.html">
                store.coffee
              </a>
            
              
              <a class="source" href="util.html">
                util.coffee
              </a>
            
              
              <a class="source" href="blenders.html">
                blenders.coffee
              </a>
            
              
              <a class="source" href="filters.html">
                filters.coffee
              </a>
            
              
              <a class="source" href="size.html">
                size.coffee
              </a>
            
              
              <a class="source" href="blur.html">
                blur.coffee
              </a>
            
              
              <a class="source" href="camera.html">
                camera.coffee
              </a>
            
              
              <a class="source" href="compoundBlur.html">
                compoundBlur.coffee
              </a>
            
              
              <a class="source" href="edges.html">
                edges.coffee
              </a>
            
              
              <a class="source" href="posterize.html">
                posterize.coffee
              </a>
            
              
              <a class="source" href="presets.html">
                presets.coffee
              </a>
            
              
              <a class="source" href="rotate.html">
                rotate.coffee
              </a>
            
              
              <a class="source" href="stackBlur.html">
                stackBlur.coffee
              </a>
            
              
              <a class="source" href="threshold.html">
                threshold.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>io.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <p>Various I/O based operations</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Caman</span>.<span class="title">IO</span></span></pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Used for parsing image URLs for domain names.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@domainRegex</span>: <span class="regexp">/(?:(?:http|https):\/\/)((?:\w+)\.(?:(?:\w|\.)+))/</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Is the given URL remote?
If a cross-origin setting is set, we assume you have CORS
properly configured.</p>
<p>@param [DOMObject] img The image to check.
@return [Boolean]</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@isRemote</span>: (img) -&gt;
    <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">unless</span> img?
    <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">if</span> <span class="property">@corsEnabled</span>(img)
    <span class="keyword">return</span> <span class="property">@isURLRemote</span> img.src</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Given an image, we check to see if a CORS policy has been defined.
@param [DOMObject] img The image to check.
@return [Boolean]</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@corsEnabled</span>: (img) -&gt;
    img.crossOrigin? <span class="keyword">and</span> img.crossOrigin.toLowerCase() <span class="keyword">in</span> [<span class="string">'anonymous'</span>, <span class="string">'use-credentials'</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Does the given URL exist on a different domain than the current one?
This is done by comparing the URL to <code>document.domain</code>.
@param [String] url The URL to check.
@return [Boolean]</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@isURLRemote</span>: (url) -&gt;
    matches = url.match <span class="property">@domainRegex</span>
    <span class="keyword">return</span> <span class="keyword">if</span> matches <span class="keyword">then</span> matches[<span class="number">1</span>] <span class="keyword">isnt</span> document.domain <span class="keyword">else</span> <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Checks to see if the URL is remote, and if there is a proxy defined, it
@param [String] src The URL to check.
@return [String] The proxy URL if the image is remote. Nothing otherwise.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@remoteCheck</span>: (src) -&gt;
    <span class="keyword">if</span> <span class="property">@isURLRemote</span> src
      <span class="keyword">if</span> <span class="keyword">not</span> Caman.remoteProxy.length
        Log.info <span class="string">"Attempting to load a remote image without a configured proxy. URL: <span class="subst">#{src}</span>"</span>
        <span class="keyword">return</span>
      <span class="keyword">else</span>
        <span class="keyword">if</span> Caman.isURLRemote Caman.remoteProxy
          Log.info <span class="string">"Cannot use a remote proxy for loading images."</span>
          <span class="keyword">return</span>
          
        <span class="keyword">return</span> <span class="property">@proxyUrl</span>(src)</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>Given a URL, get the proxy URL for it.
@param [String] src The URL to proxy.
@return [String] The proxy URL.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@proxyUrl</span>: (src) -&gt;
    <span class="string">"<span class="subst">#{Caman.remoteProxy}</span>?<span class="subst">#{Caman.proxyParam}</span>=<span class="subst">#{encodeURIComponent(src)}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Shortcut for using one of the bundled proxies.
@param [String] lang String identifier for the proxy script language.
@return [String] A proxy URL.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="property">@useProxy</span>: (lang) -&gt;
    langToExt =
      ruby: <span class="string">'rb'</span>
      python: <span class="string">'py'</span>
      perl: <span class="string">'pl'</span>
      javascript: <span class="string">'js'</span>

    lang = lang.toLowerCase()
    lang = langToExt[lang] <span class="keyword">if</span> langToExt[lang]?
    <span class="string">"proxies/caman_proxy.<span class="subst">#{lang}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Grabs the canvas data, encodes it to Base64, then sets the browser location to 
the encoded data so that the user will be prompted to download it.
If we&#39;re in NodeJS, then we can save the image to disk.
@see Caman</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>Caman::<span class="function"><span class="title">save</span></span> = -&gt;
    <span class="keyword">if</span> exports?
      <span class="property">@nodeSave</span>.apply @, arguments
    <span class="keyword">else</span>
      <span class="property">@browserSave</span>.apply @, arguments

Caman::<span class="function"><span class="title">browserSave</span></span> = (type = <span class="string">"png"</span>) -&gt;
    type = type.toLowerCase()</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Force download (its a bit hackish)</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    image = <span class="property">@toBase64</span>(type).replace <span class="string">"image/<span class="subst">#{type}</span>"</span>, <span class="string">"image/octet-stream"</span>
    document.location.href = image

Caman::<span class="function"><span class="title">nodeSave</span></span> = (file, overwrite = <span class="literal">true</span>) -&gt;
    <span class="keyword">try</span>
      stats = fs.statSync file
      <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">if</span> stats.isFile() <span class="keyword">and</span> <span class="keyword">not</span> overwrite
    <span class="keyword">catch</span> e
      Log.debug <span class="string">"Creating output file <span class="subst">#{file}</span>"</span>

    fs.writeFile file, <span class="property">@canvas</span>.toBuffer(), -&gt;
      Log.debug <span class="string">"Finished writing to <span class="subst">#{file}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Takes the current canvas data, converts it to Base64, then sets it as the source 
of a new Image object and returns it.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>Caman::<span class="function"><span class="title">toImage</span></span> = (type) -&gt;
    img = document.createElement <span class="string">'img'</span>
    img.src = <span class="property">@toBase64</span> type
    img.width = <span class="property">@dimensions</span>.width
    img.height = <span class="property">@dimensions</span>.height

    <span class="keyword">if</span> window.devicePixelRatio
      img.width /= window.devicePixelRatio
      img.height /= window.devicePixelRatio

    <span class="keyword">return</span> img</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Base64 encodes the current canvas</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>Caman::<span class="function"><span class="title">toBase64</span></span> = (type = <span class="string">"png"</span>) -&gt;
    type = type.toLowerCase()
    <span class="keyword">return</span> <span class="property">@canvas</span>.toDataURL <span class="string">"image/<span class="subst">#{type}</span>"</span>

IO = Caman.IO</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
